package com.jctrip.hotel.source.controller;

import java.util.List;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.jctrip.hotel.common.context.BeanContext;
import com.jctrip.hotel.domain.dao.HtOrderSourceRecordDao;
import com.jctrip.hotel.domain.entity.HtOrderSourceRecord;
import com.jctrip.hotel.source.model.CnBooking.resp.OrderSearchResp;
import com.jctrip.hotel.source.model.CnBooking.resp.QueryCityResp;
import com.jctrip.hotel.source.model.CnBooking.resp.ValiadRoomStatusResp;
import com.jctrip.hotel.source.model.Result;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jctrip.hotel.source.model.CnBooking.CityDataDTO;
import com.jctrip.hotel.source.model.CnBooking.IdentityInfoDTO;
import com.jctrip.hotel.source.model.CnBooking.req.BookingReq;
import com.jctrip.hotel.source.model.CnBooking.resp.BookingResp.BookingOrderData;
import com.jctrip.hotel.source.model.CnBooking.resp.QueryHotelResp.HotelsData;
import com.jctrip.hotel.source.model.CnBooking.resp.QueryRoomStatusResp.RoomStatusHotel;
import com.jctrip.hotel.source.model.CnBooking.resp.QueryRoomTypeResp.RoomTypeData;
import com.jctrip.hotel.source.model.wehotel.req.OrderData;
import com.jctrip.hotel.source.service.CNBooking.CNBookingService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author henry
 * @Date 2023/8/1 18:56
 * @Description
 */
@Slf4j
@Api(tags = "cnbooking")
@RestController
@RequestMapping("/cnbooking")
@ApiSupport(order = 2)
public class CNBookController {
    @Resource
    private CNBookingService cnBookingService;


    @RequestMapping(value = "/getCityData", method = RequestMethod.POST)
    @ApiOperation(value = "getCityData", notes = "城市数据")
    public Result getCityData(@RequestBody IdentityInfoDTO info) {
        List<CityDataDTO> result;
        try {
            result = cnBookingService.getCityData(info);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @RequestMapping(value = "/queryHotel", method = RequestMethod.POST)
    @ApiOperation(value = "queryHotel", notes = "酒店数据")
    public Result queryHotel(@RequestBody IdentityInfoDTO info,
                             @RequestParam(required = false) String countryId,
                             @RequestParam(required = false) String provinceId,
                             @RequestParam(required = false) String cityId,
                             @RequestParam(required = false) String lang,
                             @RequestParam(required = false) String hotelId,
                             @RequestParam(required = false) Integer current,
                             @RequestParam(required = false) Integer pageSize) {
        HotelsData result;
        try {
            result = cnBookingService.queryHotel(countryId, provinceId, cityId, lang, current, pageSize, hotelId, info);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @RequestMapping(value = "/queryRoomType", method = RequestMethod.POST)
    @ApiOperation(value = "queryRoomType", notes = "房型")
    public Result queryRoomType(@RequestBody IdentityInfoDTO info,
                                @RequestParam(required = false) String countryId,
                                @RequestParam(required = false) String lang,
                                @RequestParam(required = false) String hotelId) {
        RoomTypeData result;
        try {
            result = cnBookingService.queryRoomType(countryId, hotelId, lang, info);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @RequestMapping(value = "/queryRoomStatus", method = RequestMethod.POST)
    @ApiOperation(value = "queryRoomStatus", notes = "房态")
    public Result queryRoomStatus(@RequestBody IdentityInfoDTO info,
                                  @RequestParam(required = false) String countryId,
                                  @RequestParam(required = false) String roomId,
                                  @RequestParam(required = false) String ratePlanId,
                                  @RequestParam(required = false) String start,
                                  @RequestParam(required = false) String end,
                                  @RequestParam(required = false) String currency,
                                  @RequestParam(required = false) String hotelId) {
        RoomStatusHotel result;
        try {
            result = cnBookingService.queryRoomStatus(countryId, currency, hotelId, roomId, ratePlanId, start, end, info);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @RequestMapping(value = "/valiadBook", method = RequestMethod.POST)
    @ApiOperation(value = "valiadBook", notes = "下单试校验")
    public Result valiadBook(@RequestBody IdentityInfoDTO info,
                             @RequestParam(required = false) String hotelId,
                             @RequestParam(required = false) String roomId,
                             @RequestParam(required = false) String ratePlanId,
                             @RequestParam(required = false) String roomCount,
                             @RequestParam(required = false) String start,
                             @RequestParam(required = false) String end,
                             @RequestParam(required = false) String currency,
                             @RequestParam(required = false) String orderAmount) {
        ValiadRoomStatusResp.ValiadRoomStatusInfo result;
        try {
            result = cnBookingService.valiadRoomstatus(hotelId, roomId, ratePlanId, roomCount, start, end, currency, orderAmount, info);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @RequestMapping(value = "/booking", method = RequestMethod.POST)
    @ApiOperation(value = "booking", notes = "下单")
    public Result booking(@RequestBody BookingReq req) {
        BookingOrderData result;
        try {
            result = cnBookingService.booking(req);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @RequestMapping(value = "/queryOrder", method = RequestMethod.POST)
    @ApiOperation(value = "queryOrder", notes = "查询订单")
    public Result queryOrder(@RequestBody IdentityInfoDTO info, @RequestParam(required = false) String orderId,
                             @RequestParam(required = false) String outOrderNo) {
        OrderData result;
        try {
            result = cnBookingService.queryOrder(info, orderId, outOrderNo);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }

        return Result.ok(result);
    }

    @RequestMapping(value = "/cancelOrder", method = RequestMethod.POST)
    @ApiOperation(value = "cancelOrder", notes = "取消订单")
    public Result orderCancel(@RequestBody IdentityInfoDTO info, @RequestParam String orderId) {
        try {
            cnBookingService.cancelOrder(info, orderId);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok();
    }


    // ======================数据直连部分==================
    @RequestMapping(value = "/queryOrderDetail", method = RequestMethod.GET)
    @ApiOperation(value = "queryOrderDetail", notes = "直连查询订单信息")
    public Result queryOrderDetail(@RequestParam(value = "sourceId") @ApiParam("供应商账户id") String sourceId,
                                   @RequestParam(value = "outOrderNo") @ApiParam("渠道预定单号") String outOrderNo) {
        OrderSearchResp.QueryOrderData queryOrderData;
        try {
            HtOrderSourceRecordDao htOrderSourceRecordDao =
                    BeanContext.getApplicationContext().getBean(HtOrderSourceRecordDao.class);
            HtOrderSourceRecord sourceRecord = htOrderSourceRecordDao.selectOne(new LambdaQueryWrapper<HtOrderSourceRecord>()
                    .eq(HtOrderSourceRecord::getBookId, outOrderNo.trim()));
            queryOrderData = cnBookingService.queryCNBEOrder(sourceId, sourceRecord.getSourceOrderId(), outOrderNo.trim());
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(queryOrderData);
    }

}
